package org.jeecg.modules.caiGgl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.data.style.TableStyle;
import com.deepoove.poi.policy.DynamicTableRenderPolicy;
import com.deepoove.poi.policy.MiniTableRenderPolicy;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.caiGgl.entity.HqCggl;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysDictItem;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysDictItemService;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DetailTablePolicy extends DynamicTableRenderPolicy {
    int goodsStartRow = 2;
    TableStyle rowStyle = new TableStyle();

    @Autowired
    ISysDepartService sysDepartService;

    @Autowired
    ISysDictItemService sysDictItemService;

    @Override
    public void render(XWPFTable table, Object data) {
        rowStyle = new TableStyle();
        rowStyle.setAlign(STJc.CENTER);
        int startRow = 2;

        List<HqCggl> list = (List<HqCggl>)data;

        table.removeRow(startRow);
        int index = 0;
        for (int i = 0; i < list.size(); i++) {

            XWPFTableRow insertNewTableRow = table.insertNewTableRow(startRow);
            for (int j = 0; j < 14; j++)
                insertNewTableRow.createCell();

            HqCggl hqCggl = list.get(i);
            SysDepart sysDepart = sysDepartService.getOne(new QueryWrapper<SysDepart>().eq("org_code2", hqCggl.getXuqdw() == null ? "" : hqCggl.getXuqdw()), false);
            String xuqdw = "";
            if(sysDepart != null)
                xuqdw = sysDepart.getDepartName();

            String caigFs = "";
            SysDictItem sysDictItem = sysDictItemService.getOne(new QueryWrapper<SysDictItem>()
                    .eq("dict_id", "1273274179608952834")
                    .eq("item_value", hqCggl.getCaigfs()), false);
            if(sysDictItem != null)
                caigFs = sysDictItem.getItemText();

            String jiaohqx = "";
            if(hqCggl.getJaohqx() != null)
                jiaohqx = DateUtils.formatDate(hqCggl.getJaohqx());

            RowRenderData row = RowRenderData.build(++index + "",hqCggl.getId(), hqCggl.getProjectname(), hqCggl.getYuskm(), hqCggl.getGuigxh(),
                    "技术参数", hqCggl.getJildw(), hqCggl.getCaigsl() + "", hqCggl.getDanj() + "", hqCggl.getYusje() + "", jiaohqx,
                    caigFs, xuqdw, hqCggl.getBaozqd());

            row.setRowStyle(rowStyle);

            MiniTableRenderPolicy.Helper.renderRow(table, startRow, row);
        }
    }
}
